package com.ljx.demo.security.custom;

import com.alibaba.fastjson.JSON;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: LiJunXi
 * @Description: 自定义退出登录
 * @Date: 2024-06-16  23:15
 */
@Component
public class CustomLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        // 清除Redis缓存的用户数据
        // 写入用户操作日志
        // ...

        response.setStatus(HttpStatus.OK.value());
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");

        Map<String, String> res = new HashMap<>();
        res.put("code", "200");
        res.put("msg", "退出成功");

        response.getWriter().print(JSON.toJSON(res));
    }
}
